import DestructibleFlyer from "../flyer/DestructibleFlyer";
import { Killer } from "../funcObj/interface/HealthPoint";
import Launchable from "../funcObj/interface/Launchable";
import { FacilityType, GamePropType } from "../info/GameEnum";
import { Global } from "../info/Global";
import { Location } from "../info/Predefine";
import GameManager from "../manager/GameManager";
import PlayerManager from "../manager/PlayerManager";
import Player from "../player/Player";
import DestructibleFacility from "./DestructibleFacility";
const { ccclass, property } = cc._decorator;

/**
 * 可发射东西的基地设施
 */
@ccclass
export default abstract class LaunchableFalicity extends DestructibleFacility implements Launchable {

    @property(cc.ProgressBar)
    CreatingProgress: cc.ProgressBar = null;

    /**
     * 生成一个飞行物
     */
    abstract creat(): void;

    public abstract get count(): number;

    public get shotRange(): number {
        let _shotRange = this.facilityInfo.射程半径;
        let extraRate = Global.FacilityInfo[FacilityType.主基地].炮弹射程.每等级加成[this.player.shellRangeBonusLevel] / 100;
        let result = Math.ceil(_shotRange * extraRate);
        if (this.player.props[GamePropType.远程打击]) {
            result = Math.ceil(result * 1.5);
        }
        return Math.ceil(_shotRange * extraRate);
    }

    public get makingTime(): number {
        let result = this.facilityInfo.制造所需时间;
        if (this.player.props[GamePropType.弹药加速]) {
            result = Math.ceil(result * 1.5);
        }
        return result;
    }

    dispatchQueue: { flyer: cc.Node, target: Location, dispatchFunc?: (target: Location) => void }[] = null;

    dispatching: boolean = false;

    init(player: Player, ...args: any[]);
    init(player: Player): void {
        super.init(player);
        this.CreatingProgress.progress = 0;
        this.CreatingProgress.node.opacity = this.player == PlayerManager.instance.controllerPlayer ? 255 : 0;
        this.CreatingProgress.node.active = false;
        this.dispatchQueue = [];
        this.dispatching = false;
    }

    /**
     * 发射可飞行物
     * @param target 
     */
    public dispatch(target: Location) {
        //给目标发送一个被攻击事件
        let targetFacility = GameManager.instance.getFacilityByLocation(target);
        if (targetFacility && targetFacility instanceof DestructibleFacility && targetFacility.player) {
            targetFacility.player.node.emit(Player.EventType.被设定为目标, this, targetFacility);
        }
    }

    protected update(dt: number): void {
        this.autoDispatch();
    }

    /**
     * 自动发射
     */
    protected autoDispatch() {
        if (!this.dispatchQueue.length || this.dispatching) return;
        this.dispatching = true;
        let flyerInfo = this.dispatchQueue.shift();
        if (flyerInfo.dispatchFunc) {
            flyerInfo.dispatchFunc(flyerInfo.target);
        } else {
            flyerInfo.flyer.getComponent(DestructibleFlyer).dispatch(flyerInfo.target);
        }
        flyerInfo.flyer.once(DestructibleFacility.EventType.发射成功, this.onDispatchSuccess, this);
    }

    onDispatchSuccess() {
        this.dispatching = false;
    }

    die(killer: Killer): void {
        super.die(killer);
        cc.Tween.stopAllByTarget(this.CreatingProgress);
        this.dispatching = false;
        this.dispatchQueue = [];
        this.CreatingProgress.unscheduleAllCallbacks();
    }

}